#include <iostream>
#include <vector>
using namespace std;

class Solution
{
public:
    int numSquares(int n)
    {
        vector<int> sqr;
        for (int i = 1;; i++)
        {
            sqr.push_back(i * i);
            if (sqr.back() > n)
                break;
        }
        vector<int> dp(n + 1, -1);
        dp[0] = 0;
        dp[1] = 1;
        return dfs(n, sqr, dp);
    }

    int dfs(int n, vector<int> &sqr, vector<int> &dp)
    {
        if (n < 0)
            return -2;
        if (dp[n] != -1)
            return dp[n];
        for (int i = 0; i < sqr.size() && sqr[i] <= n; i++)
        {
            int ret = dfs(n - sqr[i], sqr, dp);
            if (ret >= 0)
                if (dp[n] < 0)
                    dp[n] = ret + 1;
                else
                    dp[n] = min(dp[n], ret + 1);
        }
        if (dp[n] == -1)
            dp[n] = -2;
        return dp[n];
    }
};

int main()
{
    cout << "Code279" << endl;
    Solution s;
    cout << s.numSquares(12) << endl;
    return 0;
}